﻿using System;

namespace CzfUDP
{
	public static class CzfUDPTool
	{
		public const byte SYN = 0;          // 要求建立联机
		public const byte ACK_SYN = 1;      // 回应要求建立联机
		public const byte ACK_ACK_SYN = 2;  // 回应回应要求建立联机
		public const byte PSH = 3;          // 有DATA数据传输
		public const byte ACK = 4;          // 响应
		public const byte CLOSE = 5;        // 要求直接断开连接

		public static byte[] AddHead(byte[] bytes, int i)
		{
			byte[] bts = BitConverter.GetBytes (i);
			var ret = new byte[bytes.Length + bts.Length];
			Array.Copy (bts, ret, bts.Length);
			Array.Copy (bytes, 0, ret, bts.Length, bytes.Length);
			return ret;
		}

		public static byte[] AddHead(byte[] bytes, uint i)
		{
			byte[] bts = BitConverter.GetBytes (i);
			var ret = new byte[bytes.Length + bts.Length];
			Array.Copy (bts, ret, bts.Length);
			Array.Copy (bytes, 0, ret, bts.Length, bytes.Length);
			return ret;
		}

		public static byte[] AddHead(byte[] bytes, byte bt)
		{
			var ret = new byte[bytes.Length + 1];
			ret [0] = bt;
			Array.Copy (bytes, 0, ret, 1, bytes.Length);
			return ret;
		}

		public static byte[] RemoveHead(byte[] bytes, int length)
		{
			var ret = new byte[bytes.Length - length];
			Array.Copy (bytes, length, ret, 0, (bytes.Length - length));
			return ret;
		}

		public static byte[] Sub(byte[] bytes, int start)
		{
			return Sub (bytes, start, bytes.Length - start);
		}

		public static byte[] Sub(byte[] bytes, int start, int length)
		{
			var maxLength = bytes.Length - start;
			length = Math.Min (maxLength, length);
			var ret = new byte[length];
			Array.Copy (bytes, start, ret, 0, length);
			return ret;
		}
	}
}

